/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Cobalt.Content.Pipeline.Graphics
{
	[ContentTypeWriter]
	public class CobaltMaterialContentWriter : ContentTypeWriter<CobaltMaterialContent>
	{
		private void WriteParameters( ContentWriter output, MaterialContent value )
		{
			Dictionary<string, object> props = new Dictionary<string, object>();
			foreach( KeyValuePair<string, object> pair in value.OpaqueData )
			{
				if( pair.Key != "Effect" && pair.Key != "CompiledEffect" )
					props.Add( pair.Key, pair.Value );
			}

			foreach( KeyValuePair<string, ExternalReference<TextureContent>> pair in value.Textures )
			{
				props.Add( pair.Key, pair.Value );
			}

			output.WriteObject( props );
		}

		protected override void Write( ContentWriter output, CobaltMaterialContent value )
		{
			output.Write( (int)0 ); //inline material

			output.Write( value.Name ?? "[Unnamed Material]" );

			WriteParameters( output, value );

			foreach( KeyValuePair<int, EffectMaterialContent> pair in value.Layers )
			{
				if( pair.Value == null )
					continue;

				output.Write( pair.Key );

				output.WriteExternalReference( pair.Value.CompiledEffect );
				WriteParameters( output, pair.Value );
			}

			output.Write( (int)-1 );

			Helpers.WriteTags( output, value.Tags );
		}

		public override string GetRuntimeType( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.Material" );
		}

		public override string GetRuntimeReader( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.MaterialReader" );
		}
	}

	[ContentTypeWriter]
	public class ExternalCobaltMaterialContentWriter : ContentTypeWriter<ExternalCobaltMaterialContent>
	{
		protected override void Write( ContentWriter output, ExternalCobaltMaterialContent value )
		{
			output.Write( (int)1 ); //is an external reference
			output.WriteExternalReference( value.Reference );
		}

		public override string GetRuntimeType( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.Material" );
		}

		public override string GetRuntimeReader( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.MaterialReader" );
		}
	}

	[ContentTypeWriter]
	public class NamedCobaltMaterialContentWriter : ContentTypeWriter<NamedCobaltMaterialContent>
	{
		protected override void Write( ContentWriter output, NamedCobaltMaterialContent value )
		{
			output.Write( (int)2 ); //is a simple name
			output.Write( value.Name );
		}

		public override string GetRuntimeType( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.Material" );
		}

		public override string GetRuntimeReader( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.MaterialReader" );
		}
	}
}
